package com.czmh.etc.onlinepubsys.utils;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

/**
 * @ClassName：StringHexUtil
 * @Description：TODO @Author： sevenchen. @Date：2019/6/14 01:10. @Version：1.0.0
 */
public class StringHexUtil {
    public static void main(String[] args) throws UnsupportedEncodingException, DecoderException, ParseException {
        System.out.println(
                hexStr2Str("840a",
                        "GB2312"));
        System.out.println(hexStr2Str("6f40840ab9e3b6abb8dfcbd93031a5329f0801029f0c2bb9e3b6ab440100011704440119252333565739302019083120240830d4c1575158323535000000000100009000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff9000", "GB2312"));
        System.out.println(hexStr2Str("86be000213419cca", "GB2312"));
        String ef01 = "b9f3d6dd00010001161086be000213419cca2008040820200101009000";
        int m = ef01.length() / 2;
        if (m * 2 < ef01.length()) {
            m++;
        }
        String[] strs = new String[m];
        int j = 0;
        for (int i = 0; i < ef01.length(); i++) {
            if (i % 2 == 0) {// 每隔两个
                strs[j] = "" + ef01.charAt(i);
            } else {
                strs[j] = strs[j] + ef01.charAt(i);
                j++;
            }
        }
        
        String serialNumber = "";
        for (int i = 10; i < 17; i++) {
            serialNumber = serialNumber + strs[i];
        }
        String splitState = strs[27];
        System.out.println(serialNumber);
        System.out.println(Arrays.toString(strs));
    }

    
    public static String getChinese(String url){
        String regex = "([\u4e00-\u9fa5]+)";
        Matcher matcher = Pattern.compile(regex).matcher(url);
        String result = null;
        if(matcher.find()){
            result = matcher.group(1);
        }
        return result;
    }

    /**
     * 将16进制字符串转换为普通字符串
     * 
     * @param hexStr
     *            16进制字符串
     * @param charset
     *            编码格式 可不传默认为Charset.defaultCharset()
     * @return
     * @throws DecoderException
     * @throws UnsupportedEncodingException
     */
    public static String hexStr2Str(String hexStr, String charset)
            throws DecoderException, UnsupportedEncodingException {
        byte[] bytes = Hex.decodeHex(hexStr);
        return new String(bytes, charset);
    }

    /**
     * 16进制直接转换成为字符串(无需Unicode解码)
     *
     * @param hexStr
     * @return
     */
    public static String hexStr2Str(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }

    /**
     * 字符串转换成为16进制(无需Unicode编码)
     *
     * @param str
     * @return
     */
    public static String str2HexStr(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            // sb.append(' ');
        }
        return sb.toString().trim();
    }

    /**
     * unicode 转字符串
     */
    public static String unicode2String(String unicode) {
        StringBuffer string = new StringBuffer();
        String[] hex = unicode.split("\\\\u");
        for (int i = 1; i < hex.length; i++) {
            // 转换出每一个代码点
            int data = Integer.parseInt(hex[i], 16);
            // 追加成string
            string.append((char) data);
        }
        return string.toString();
    }

    /**
     * 16进制转换成为string类型字符串
     *
     * @param s
     * @return
     */
    public static String hexStringToString(String s) {
        if (s == null || "".equals(s)) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "GB2312");
            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    /**
     * 字符串转化成为16进制字符串
     *
     * @param s
     * @return
     */
    public static String strTo16(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            String s4 = Integer.toHexString(ch);
            str = str + s4;
        }
        return str;
    }

    /**
     * 字符串转换unicode
     */
    public static String string2Unicode(String string) {
        StringBuffer unicode = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            // 取出每一个字符
            char c = string.charAt(i);
            // 转换为unicode
            unicode.append("\\u" + Integer.toHexString(c));
        }
        return unicode.toString();
    }
}
